package org.bonorsoft.modules.wechat.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.system.base.controller.BonorController;
import org.bonorsoft.common.system.query.QueryGenerator;
import org.bonorsoft.common.system.vo.LoginUser;
import org.bonorsoft.common.util.oConvertUtils;
import org.bonorsoft.modules.wechat.entity.NywxHf;
import org.bonorsoft.modules.wechat.entity.NywxTj;
import org.bonorsoft.modules.wechat.service.INywxHfService;
import org.bonorsoft.modules.wechat.service.INywxPhotoService;
import org.bonorsoft.modules.wechat.service.INywxTjService;
import org.bonorsoft.modules.wechat.vo.NywxTjVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;

/**
 * 你有我需 小程序端
 */
@Slf4j
@Controller
@RequestMapping("/wechat/nywx")
public class WxNywxController extends BonorController<NywxTj, INywxTjService> {
    @Autowired
    private INywxTjService nywxTjService;
    @Autowired
    private INywxHfService nywxHfService;
    @Autowired
    private INywxPhotoService nywxPhotoService;

    /**
     * 你有我需 分页列表查询
     * @param nywx
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "你有我需-分页列表查询")
    @RequiresPermissions("control:nywx:list")
    @GetMapping(value = "/list")
    @ResponseBody
    public Result<?> queryPageList(NywxTj nywx,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<NywxTj> queryWrapper = QueryGenerator.initQueryWrapper(nywx, req.getParameterMap());
        Page<NywxTj> page = new Page<NywxTj>(pageNo, pageSize);
        IPage<NywxTj> pageList = nywxTjService.paging(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 你有我需 新增
     * @param nywxTj
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result<?> add(@RequestBody NywxTjVo nywxTj) {
        String photo = nywxTj.getPhoto();
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();//取得当前用户
        String qqid = UUID.randomUUID().toString().replaceAll("-", "");
        nywxTj.setTjid(qqid).setTjrid(user.getUsername()).setTjrxm(user.getRealname()).setTjsj(new Date());
        nywxTjService.save(nywxTj);
        if (oConvertUtils.isNotEmpty(photo)) {
            String[] split;
            if (photo.contains(",")) {
                split = photo.split(",");
            } else {
                split = new String[]{photo};
            }
            for (String s : split) {
                nywxPhotoService.addPhoto(qqid, s);
            }
        }
        return Result.ok("添加成功！");
    }

    /**
     * 你有我需 回复
     *
     * @param nywxHf
     * @return
     */
    @RequestMapping(value = "/hfAdd", method = RequestMethod.POST)
    @ResponseBody
    public Result<?> hfAdd(@Validated NywxHf nywxHf) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();//取得当前用户
        String hfid = UUID.randomUUID().toString().replaceAll("-", "");
        nywxHf.setHfid(hfid).setHfrid(user.getUsername()).setHfrxm(user.getRealname()).setHfsj(new Date());
        nywxHfService.save(nywxHf);
        return Result.ok("添加成功！");
    }

    /**
     * 你有我需 详细
     * @param req
     * @return
     */
    @GetMapping("/detail")
    @ResponseBody
    public Result<?> detail(HttpServletRequest req){
        String id = req.getParameter("id");
        Result<JSONObject> result = new Result<>();
        JSONObject obj = new JSONObject();
        obj.put("nywxTj",nywxTjService.getById(id));
        obj.put("photo",nywxPhotoService.getPhoto(id));
        obj.put("nywxHf",nywxHfService.getNywxHfByTjid(id));
        result.setResult(obj);
        result.success("查询成功！");
        return result;
    }


}
